import re

from qturboframe.lib.report.log import logger
from qturboframe.public_ui.base_element import BaseElement
from qturboframe.lib.webdriver import SeleniumError


class HomeTab(BaseElement):
    '''
    首页的tab标签类
    '''

    def __init__(self, tab_name, idname, father, timeout=5):
        super().__init__(idname, father, timeout=timeout)
        self.topdiv = self._element
        self.tabs = None        # tab元素列表
        self.text_tabs = None   # tab 文本列表
        self.__get_tabs()       # 获取一下当前的tab栏的tab信息
        # modules = self.topdiv.find_elements_by_attribute(
        #     'css selector', 'a.h-navbar-tab', 'text:' + tab_name, timeout=timeout)
        modules = self.topdiv.find_elements_by_attribute(
            'xpath', f'//a[contains(@class,"h-navbar-tab")]/div/span[contains(text(),"{tab_name}")]/../..',
            timeout=timeout)
        logger.debug(f'Result of target tab is : {modules}')
        if modules:
            self._element = modules[0]
        else:
            self._element = None

    def is_open(self):
        if self._element:
            return True
        else:
            return False

    def __get_tabs(self):
        '''
        获取当前已经打开的菜单列表
        :return:
        '''
        if self.topdiv:
            element_tabs = self.topdiv.find_elements_by_attribute('css selector', 'a.h-navbar-tab', timeout=1)
            if element_tabs:
                self.tabs = element_tabs
                self.text_tabs = [unit.text for unit in element_tabs]
                logger.info(f'当前页面已有的tab：{self.text_tabs}')
                return self.text_tabs
            else:
                return None
        else:
            return None

    def get_tabs_text(self):
        self.__get_tabs()
        return self.text_tabs

    def get_cur_active(self):
        '''
        获取当前活跃的tab页
        :return:
        '''
        tar_div = self.topdiv.exist('css selector', 'a.h-navbar-tab.is-active', timeout=0.3)
        if tar_div:
            cur_tab = tar_div.text
        else:
            cur_tab = ''
        return cur_tab

    def check_tab(self, expect_tab):
        cur_tab = self.get_cur_active()
        if cur_tab and re.search(expect_tab, cur_tab):
            logger.info(f'检查当前tab：{cur_tab}和期望的一致。')
        elif cur_tab and not re.search(expect_tab, cur_tab):
            logger.error(f'检查当前tab：{cur_tab}和期望的tab{expect_tab}不一致。')
            raise SeleniumError(f'检查当前tab：{cur_tab}和期望的tab{expect_tab}不一致。')
        else:
            logger.error(f'当前tab为空。')
            raise SeleniumError(f'当前tab为空。')

    def switch_tab(self, tab_name):
        '''
        打开目标的tab
        :param tab_name:
        :return:
        '''
        if self.text_tabs and tab_name in self.text_tabs:
            for unit in self.tabs:
                if unit.text == tab_name:
                    self._element = unit
                    self.select()
                    logger.info(f'switch tab {tab_name}')
                    return True
            else:
                logger.warning(f'switch tab {tab_name} fail, not found!')
                return False
        else:
            logger.warning(f'switch tab {tab_name} fail, no tab!')
            return False


    def select(self):
        '''
        选择选项卡
        :return:
        '''
        self._element.secure_click()
        logger.info('打开选项卡。')

    def close(self):
        '''
        关闭当前选项卡
        :return:
        '''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'section.h-navbar-context-menu-item',
                                                     'text:关闭当前页', timeout=self.timeout)
        upul.secure_click()
        logger.info('关闭当前页')

    def close_others(self):
        '''
        关闭当前意外选项卡
        :return:
        '''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'section.h-navbar-context-menu-item',
                                                     'text:关闭全部页', timeout=self.timeout)
        upul.secure_click()
        logger.info('关闭全部页')

    def close_all(self):
        '''
        关闭全部选项卡
        :return:
        '''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'section.h-navbar-context-menu-item',
                                                     'text:关闭全部页', timeout=self.timeout)
        upul.secure_click()
        logger.info('关闭全部页。')

    def reload(self):
        '''
        刷新当前页面
        :return:
        '''
        self._element.right_click()
        upul = self.topdiv.find_element_by_attribute('css selector', 'section.h-navbar-context-menu-item',
                                                     'text:刷新当前页', timeout=self.timeout)
        upul.secure_click()
        logger.info('刷新当前页。')